/**
 * 
 */
package com.yst.metho.web.application.components;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

import nextapp.echo2.app.Button;
import nextapp.echo2.app.Extent;
import nextapp.echo2.app.ListBox;
import nextapp.echo2.app.Row;
import nextapp.echo2.app.event.ActionEvent;
import nextapp.echo2.app.event.ActionListener;
import nextapp.echo2.app.event.ChangeEvent;
import nextapp.echo2.app.event.ChangeListener;
import nextapp.echo2.app.list.ListCellRenderer;
import nextapp.echo2.app.list.ListSelectionModel;

import com.yst.metho.web.application.components.doublelist.DoubleListControlPane;
import com.yst.metho.web.application.components.doublelist.DoubleListModel;

/**
 * @author perso
 * 
 */
public class DoubleListComponent extends Row
{
	private class ButtonController extends Button implements ChangeListener
	{

		public ButtonController(String msg)
		{
			super(msg);
			setEnabled(true);
		}

		public void stateChanged(ChangeEvent e)
		{
			ListSelectionModel model = (ListSelectionModel) e.getSource();
			setEnabled(!model.isSelectionEmpty());
		}

	}

	private ListBox mBoxSelected;

	private ListBox mBoxAvailable;

	private ArrayList mSelectedList;

	private ArrayList mAvailableList;

	private DoubleListModel mAvailableModel;

	private DoubleListModel mSelectedModel;

	public DoubleListComponent(Collection selected, Collection available, ListCellRenderer renderer)
	{
		// creation d'un dump des listes d'origine pour pouvoir les manipuler
		// sans soucis d'impact.
		mSelectedList = new ArrayList();
		dumpList(selected, mSelectedList);
		mAvailableList = new ArrayList();
		dumpList(available, mAvailableList);
		// Suppression des objets selectionnes qui sont presents dans la liste
		// des objets disponibles.
		mAvailableList.removeAll(mSelectedList);
		mSelectedModel = new DoubleListModel(mSelectedList);
		mBoxSelected = new ListBox(mSelectedModel);
		mBoxSelected.setWidth(new Extent(150, Extent.PX));
		mBoxSelected.setCellRenderer(renderer);
		mBoxSelected.setHeight(new Extent(150, Extent.PX));
		add(mBoxSelected);
		DoubleListControlPane control = new DoubleListControlPane();
		ButtonController btn = new ButtonController("->");
		btn.addActionListener(new ActionListener()
		{

			public void actionPerformed(ActionEvent e)
			{
				remove();
			}

		});
		mBoxSelected.getSelectionModel().addChangeListener(btn);
		control.add(btn);
		btn = new ButtonController("<-");
		btn.addActionListener(new ActionListener()
		{

			public void actionPerformed(ActionEvent e)
			{
				add();
			}

		});
		control.add(btn);
		add(control);
		mAvailableModel = new DoubleListModel(mAvailableList);
		mBoxAvailable = new ListBox(mAvailableModel);
		mBoxAvailable.setWidth(new Extent(150, Extent.PX));
		mBoxAvailable.setCellRenderer(renderer);
		mBoxAvailable.setHeight(new Extent(150, Extent.PX));
		mBoxAvailable.getSelectionModel().addChangeListener(btn);
		add(mBoxAvailable);
	}

	/**
	 * Supprime l'element selectionne de la liste des elements selectionnes et
	 * l'ajoute dans la liste des items disponibles.
	 * 
	 */
	private void remove()
	{
		if (mBoxSelected.getSelectedIndices().length > 0)
		{
			mAvailableList.add(mBoxSelected.getSelectedValue());
			mSelectedList.remove(mBoxSelected.getSelectedValue());
			mAvailableModel.refresh();
			mSelectedModel.refresh();
		}
	}

	/**
	 * ajoute l'element selectionne dans la liste des items disponibles dans la
	 * liste des items selectionnes. Supprime cet element de la liste des items
	 * disponibles.
	 * 
	 */
	private void add()
	{
		if (mBoxAvailable.getSelectedIndices().length > 0)
		{
			mSelectedList.add(mBoxAvailable.getSelectedValue());
			mAvailableList.remove(mBoxAvailable.getSelectedValue());
			mAvailableModel.refresh();
			mSelectedModel.refresh();
		}
	}

	/**
	 * Cree une coopie de la liste list dans la liste dump. Ceci permet de
	 * manipuler la copie sans interferer avec la liste d'origine.
	 * 
	 * @param list
	 * @param dump
	 */
	private void dumpList(Collection list, ArrayList dump)
	{
		Iterator it = list.iterator();
		while (it.hasNext())
		{
			dump.add(it.next());
		}
	}

	/**
	 * Retourne la liste de tous les elements selectionnes.
	 * 
	 * @return the selectedList
	 */
	public ArrayList getSelectedList()
	{
		return mSelectedList;
	}
}
